/*******************************************************************************
 * Copyright (c) 2011 squid.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     squid - initial API and implementation
 ******************************************************************************/
package com.google.code.cubeirc.common;


import java.io.File;
import java.lang.reflect.Constructor;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.apache.log4j.Level;
import org.eclipse.swt.widgets.Display;

import com.google.code.cubeirc.base.BaseTab;
import com.google.code.cubeirc.debug.DebuggerQueue;

/**
 * Class with commons functions
 * @author squid
 *
 */
public class Utils {


	public static String getDirSeparator()
	{
		return System.getProperty("file.separator");
	}


	/**
	 * Exec asyncThread
	 * @param display
	 * @param runnable
	 */
	public static void asyncExec(Display display, Runnable runnable)
	{
		try
		{
			if (!display.isDisposed())
				display.asyncExec(runnable);
		}
		catch(Exception ex)
		{
			DebuggerQueue.addDebug("AsyncExec" , Level.FATAL,"Error during exec AsyncExec %s error: %s", runnable.toString(), ex.getMessage());
		}
	}

	/**
	 * Function for get formatted current time
	 * for add to console.
	 *
	 * @return the current time
	 */
	public static String getCurrentTime()
	{
		DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
		Date date = new Date();
		return dateFormat.format(date);
	}
	
	public static String parseDate(long date)
	{
		DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault());
		Date dt = new Date(date);
		return dateFormat.format(dt);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static BaseTab createBaseTabfromClass(String Classname, Object[] args, Class[] params )
	{
		BaseTab control = null;
		try
		{
			DebuggerQueue.addDebug(Utils.class.getName(), Level.DEBUG, "Dynamic creation of class %s [%s]",Classname, args);
			ClassLoader plugin_class_loader = ClassLoader.getSystemClassLoader();

			try {
				Class.forName(Classname);
			} catch (ClassNotFoundException ex) {
			}


			Constructor ct;
			Class myClass = plugin_class_loader.loadClass(Classname.trim());
			
			ct = myClass.getConstructor(params);

			
			
		
			control =  (BaseTab) ct.newInstance(args);
		}
		catch(Exception ex)
		{
			DebuggerQueue.addDebug(Utils.class.getName(), Level.FATAL, "Error during casting class %s error: %s",Classname,ex.getMessage());
		}
		return control;		
	}
	
	public static boolean checkDirectoryExist(String directory, boolean create)
	{
		File file=new File(directory);
		Boolean exists = file.exists();

		if (!exists)
		{
			if (create)
			{
				new File(directory).mkdir();
				exists = true;
			}
		}
		return exists;
	}

	public static String reportTraffic(long trafficPrint){

		if((trafficPrint/1000000000)>0){
			return (trafficPrint/1000000000 + " GB");
		}

		else if((trafficPrint/1000000)>0){
			return (trafficPrint/1000000 + " MB");
		}

		else if((trafficPrint/1000)>0){
			return (trafficPrint/1000 + " kB");
		}

		else
		{
			return (trafficPrint + " byte");
		}
	}


}
